<template>
    <!-- 听歌排行榜 -->
    <div class="song-ranking">
        <header>
            <section>
                <em>听歌排行</em>
                <span>累计听歌392首</span>
                <el-icon><warning-filled /></el-icon>
            </section>
            <section>
                <span @click="toggleDate(1)" :class="week">最近一周</span>
                <span>|</span>
                <span @click="toggleDate(0)" :class="all">所有时间</span>
            </section>
        </header>
        <ul class="main">
            <li v-for="(item, index) in songRankingList" :key="item.song.id">
                <div class="number-play">
                    <span>{{ index + 1 }}.</span>
                    <span @click="play(item.song.id)"></span>
                </div>
                <div class="song">
                    <span @click="toSongDetail(item.song)">{{
                        item.song.name
                    }}</span>
                    <em>-</em>
                    <i v-for="(ar, index) in item.song.ar" :key="ar.id">
                        <span>{{ ar.name }}</span
                        ><i v-if="!(item.song.ar.length - 1 === index)">/</i>
                    </i>
                </div>
                <div class="tops">
                    <span
                        class="bg"
                        :style="'width:' + item.playCount * maxPlayCount + 'px'"
                    ></span>
                    <span class="count">{{ item.playCount }}次</span>
                </div>
            </li>
        </ul>
        <span v-if="songRankingList.length <= 10" @click="toggleDate(type, 100)"
            >查看更多>></span
        >
    </div>
</template>

<script setup>
import { getCurrentInstance, toRefs, reactive, inject } from "vue";
const axios = getCurrentInstance().proxy.$http;

const emitter = inject("emitter");

import { useRouter } from "vue-router";
const router = useRouter();

const props = defineProps({
    userId: Number,
});

// 获取最近播放的歌曲
const song_ranking = reactive({
    week: "select",
    all: "",
    songRankingList: [],
    maxPlayCount: null,
    type: 1,
});
const getSongRanking = async (type, len = 10) => {
    const { data } = await axios.get("/user/record", {
        params: {
            uid: props.userId,
            type, //1=weekData 0=allData
        },
    });
    if (type) {
        song_ranking.songRankingList = data.weekData.splice(0, len);
    } else {
        song_ranking.songRankingList = data.allData.splice(0, len);
    }
    song_ranking.maxPlayCount = 300 / song_ranking.songRankingList[0]?.playCount;
};

//切换最近一周数据或所有事件数据
const toggleDate = async (type, len) => {
    if (type) {
        //weekData
        song_ranking.week = "select";
        song_ranking.all = "";
        song_ranking.type = type;
        await getSongRanking(type, len);
    } else {
        //allData
        song_ranking.all = "select";
        song_ranking.week = "";
        song_ranking.type = type;
        await getSongRanking(type, len);
    }
};
getSongRanking(1);

const { week, all, songRankingList, maxPlayCount, type } = toRefs(song_ranking);

// 点击歌曲跳转到歌曲详情
const toSongDetail = (value) => {
    router.push({
        name: "song",
        query: { id: value.id },
    });
};

// 播放歌曲
const play = (id) => {
    emitter.emit('play', id);
};
</script>

<style lang='less' scoped>
// 听歌排行
.song-ranking {
    header {
        background-color: white;
        border-bottom: 2.5px solid rgb(182, 7, 7);
        height: 40px;
        line-height: 40px;
        color: rgb(49, 49, 49);
        section {
            &:nth-child(1) {
                display: flex;
                align-items: center;
                float: left;
                em {
                    font-size: 20px;
                    margin-right: 15px;
                }
                span {
                    font-size: 12px;
                    margin-right: 15px;
                }
                .el-icon {
                    font-size: 15px;
                    vertical-align: middle;
                }
            }
            &:nth-child(2) {
                float: right;
                font-size: 12px;
                display: flex;
                span {
                    margin-left: 10px;
                    user-select: none;
                    cursor: pointer;
                }
                .select {
                    font-weight: 700;
                    color: black;
                }
            }
        }
    }
    .main {
        margin: 0;
        padding: 0;
        width: 900px;
        border: 1px solid #ccc;
        border-top: none;
        box-sizing: border-box;
        li {
            height: 38px;
            box-sizing: border-box;
            cursor: pointer;
            &:nth-child(2n) {
                background-color: #f7f7f7;
            }
            &:nth-child(2n):hover {
                background-color: #e6e6e6;
            }
            &:nth-child(2n + 1):hover {
                background-color: #eeeeee;
            }
            .number-play {
                width: 70px;
                height: 100%;
                float: left;
                position: relative;
                span {
                    float: right;
                    font-size: 15px;
                    color: black;
                    line-height: 38px !important;
                    &:first-child {
                        margin-right: 25px;
                        color: rgb(131, 131, 131);
                    }
                    &:last-child {
                        width: 17px;
                        height: 17px;
                        display: inline-block;
                        position: absolute;
                        right: 0px;
                        margin-top: 11px;
                        background: url(../../../../assets/img/table.png) //
                            no-repeat;
                        background-position: 0 -103px;
                    }
                }
            }
            .song {
                font-size: 12px;
                line-height: 38px;
                float: left;
                span {
                    float: left;
                    line-height: 38px;
                    margin: 0 10px;
                    font-size: 12px;
                    color: black;
                    font-weight: 600;
                    float: left;
                    &:hover {
                        text-decoration: underline;
                    }
                }
                em {
                    padding: 0 5px;
                    color: #ccc;
                    float: left;
                }
                i {
                    float: left;
                    span {
                        color: rgb(121, 121, 121);
                        font-weight: normal;
                        margin: 0;
                    }
                    i {
                        padding: 0 3px;
                        color: rgb(121, 121, 121);
                    }
                }
            }

            .tops {
                float: right;
                width: 300px;
                height: 38px;
                line-height: 38px;
                color: black;
                font-size: 12px;
                position: relative;
                .bg {
                    position: absolute;
                    left: 0px;
                    background-color: #4eb4f5;
                    opacity: 0.1;
                    display: inline-block;
                    height: 100%;
                }
                .count {
                    position: absolute;
                    left: 10px;
                    top: 50%;
                    transform: translateY(-50%);
                }
            }
        }
    }
    span {
        float: right;
        font-size: 12px;
        color: #999;
        line-height: 30px;
        user-select: none;
        cursor: pointer;
    }
}
</style>